package com.jcnet.bizaia.tools;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.bean.SearchBean;
import com.jcnet.bizaia.bean.UserBean;

public class JCTools {

	static DataStorageHelper userDataStorageHelper ;
	
	private static SharedPreferences sharedPreferences;
	
	public static boolean isLogin(Context context){
		userDataStorageHelper = new DataStorageHelper(context, SPUserBean.dataTableNameForUser);
		return !(userDataStorageHelper.getInt(SPUserBean.userIDSP_1,-100)==-100);
	}
	
	
	public static UserBean getUserBean(Context context){
		
		userDataStorageHelper = new DataStorageHelper(context, SPUserBean.dataTableNameForUser);
		
		UserBean userBean = new UserBean();
		
		userBean.setNickname(userDataStorageHelper.get(SPUserBean.nikeNameSP_2, ""));
		userBean.setUsermail(userDataStorageHelper.get(SPUserBean.regmailSP_6, ""));
		userBean.setUserid(userDataStorageHelper.getInt(SPUserBean.userIDSP_1, -100));
		userBean.setUserimage(userDataStorageHelper.get(SPUserBean.userImgSP_4, ""));
		userBean.setVipsum(userDataStorageHelper.getInt(SPUserBean.vipsumSP_5, -100));
		
		return userBean;
	}
	public static int getUserID(Context context){
		
		userDataStorageHelper = new DataStorageHelper(context, SPUserBean.dataTableNameForUser);
		
		return userDataStorageHelper.getInt(SPUserBean.userIDSP_1, -400);
	}
	
	
	@SuppressLint("NewApi")
	public static void putSettingUserCategory(Context context,String name , Set<String>setStrings ){
		sharedPreferences = context.getSharedPreferences("SettingUserMenu", Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putStringSet(name, setStrings).commit();
	}
	
	@SuppressLint("NewApi")
	public static List<SearchBean> getSettingUserCategory(Context context,String name){
		sharedPreferences = context.getSharedPreferences("SettingUserMenu", Context.MODE_PRIVATE);
		Set<String>setStrings = sharedPreferences.getStringSet("name", null);
		List<SearchBean>searchBeans = new ArrayList<SearchBean>();
		for (String string : setStrings) {
			searchBeans.add(new SearchBean(string.split(",")[0], Integer.parseInt(string.split(",")[1])));
		}
		return searchBeans;
	}
	
	/**
	 * @param context
	 * @see   清除用户登陆信息
	 */
	public static void loginOut(Context context){ 
		userDataStorageHelper = new DataStorageHelper(context, SPUserBean.dataTableNameForUser);
		
		userDataStorageHelper.remove(SPUserBean.userIDSP_1,SPUserBean.nikeNameSP_2,SPUserBean.userImgSP_4,SPUserBean.vipsumSP_5,SPUserBean.regmailSP_6);
		
	}
	
	
	
	
	
	
}
